package main

import (
	"log"
	"time"
)

func main() {
	//时间打印
	timePrint()
	//after操作
	testAfterDo()
	testTicker()

}
func testTicker() {
	ticker := time.NewTicker(time.Second * 4)
	for {
		select {
		//间隔4秒将管道中的数据取出来
		case <-ticker.C:
			log.Println("间隔4秒执行一次")
		}
	}

}
func testAfterDo() {
	//1、5秒钟之后开始执行
	time.After(time.Second * 5)
	log.Println("我心情不好.")
	//2、5秒钟后开始执行,时间到了 将管道中的数据读出 后即可 执行后面的代码
	timer := time.NewTimer(time.Second * 5)
	<-timer.C
	log.Println("我心情不好!")
}

func timePrint() {
	nowtime := time.Now()
	log.Printf("now time =%v", nowtime)
	//格式必须是 2006-01-02 15:04:05
	log.Printf("now time fot=%v", nowtime.Format("2006-01-02 15:04:05"))
	log.Printf("now time year =%v", nowtime.Year())
	log.Printf("now time month =%v", nowtime.Month())
	log.Printf("now time day =%v", nowtime.Day())
	log.Printf("now time hour =%v", nowtime.Hour())
	log.Printf("now time min =%v", nowtime.Minute())
	//1 秒等于1000毫秒，1 毫秒等于1000微秒，1 微秒等于1000纳秒
	log.Printf("now time seco =%v", nowtime.Second())
	log.Printf("now time hao =%v", nowtime.Nanosecond()/1000000)
	log.Printf("now time wei =%v", nowtime.Nanosecond()/1000)
	log.Printf("now time nan =%v", nowtime.Nanosecond())
	//系统时间 的纳秒数
	log.Printf("now time UnixNan =%v", nowtime.UnixNano())
	//年月日 时分秒
	log.Printf("now time Unix =%v", nowtime.Unix())
	//100年以后
	log.Printf("100 年以后=%v", nowtime.Add(time.Hour*24*365*100))
	log.Printf("24小时之前=%v", nowtime.Add(time.Hour*-24))
	//休眠20s
	time.Sleep(time.Second * 20)

}
